package cn.yzfy.common.validate.impl;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import cn.yzfy.common.validate.ValidateDate;

import java.text.SimpleDateFormat;

public class DateValidatorImpl implements ConstraintValidator<ValidateDate, String> {
  
  private String[] formats;
  
  public DateValidatorImpl() {
  }
  
  @Override
  public void initialize(ValidateDate constraintAnnotation) {
    this.formats = constraintAnnotation.format();
  }
  
  @Override
  public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
    if (value == null || value.trim().isEmpty()) {
      return true;
    }
    
    for (String string : formats) {
      try {
        SimpleDateFormat formatter = new SimpleDateFormat(string);
        formatter.parse(value);
      } catch (Exception e) {
        return false;
      }
    }
    return true;
  }
}